package cn.darkhorse.common.report.service.impl;

import cn.darkhorse.common.report.service.DataHandlerHelper;
import cn.darkhorse.common.report.service.IConverter;
import cn.darkhorse.common.report.service.IReportDataHandler;
import com.ruoyi.common.exception.ServiceException;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanjun
 * @since 2021/8/13 16:51
 */
public class SimpleDefaultHandler<T> implements IReportDataHandler<T> {
    public static final String VAL_LIST = DataHandlerHelper.VAL_LIST;
    public static final String COL_LIST = DataHandlerHelper.COL_LIST;
    private IConverter converter;

    public SimpleDefaultHandler(IConverter converter) {
        this.converter = converter;
    }

    public SimpleDefaultHandler() {
    }

    @Override
    public Map<String, Object> handle(T dataSet) {
        throw new ServiceException("不支持该方法");
    }

    @Override
    public Map<String, Object> handle(List<T> dataSet) {
        if (null == dataSet || dataSet.size() == 0) {
            throw new ServiceException("数据集不能为空");
        }
        T obj =dataSet.get(0);

        Map<String, Object> value = doGetHeader(obj.getClass());
        doGetValue(dataSet,value);
        return value;
    }

    private void doGetValue(List<T> list, Map<String, Object> value) {
        List<Map<String, Object>> valList = DataHandlerHelper.getValList(value);
        // 填充数据

        list.forEach(ele-> {
            Map<String,Object> map = new HashMap<>();
            Class clazz = ele.getClass();
            while (clazz != null) {
                Field[] declaredFields = clazz.getDeclaredFields();
                for (int i = 0; i < declaredFields.length; i++) {
                    Field field = declaredFields[i];
                    field.setAccessible(true);

                    try {
                        Object val = field.get(ele);
                        if (null != val) {
                            map.put(field.getName(), convert(field.getName(), val));
                        } else {
                            Class<?> type = field.getType();
                            if (type.equals(BigDecimal.class)) {
                                map.put(field.getName(), convert(field.getName(), "0.00"));
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        throw new ServiceException("填充数据出错");
                    }
                }
                clazz = clazz.getSuperclass();
            }

            valList.add(map);
        });

        value.put(VAL_LIST,valList);

    }

    private Map<String, Object> doGetHeader(Class clazz) {
        Map<String, Object> value = new HashMap<String, Object>();

        // Step.1 先处理表头
        List<Map<String,Object>> colList = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        Class tempClass = clazz;
        while (tempClass != null) {
            Field[] declaredFields = tempClass.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field field = declaredFields[i];
                Class<?> type = field.getType();
                if (type.equals(BigDecimal.class)) {
                    map.put(field.getName(), "n:t." + field.getName());
                } else {
                    map.put(field.getName(), "t." + field.getName());
                }
            }
            tempClass = tempClass.getSuperclass();
        }

        colList.add(map);
        value.put(COL_LIST,colList);

        return value;
    }

    /**
     * 字典项翻译
     * @param fieldName
     * @param value
     * @return
     */
    private String convert(String fieldName,Object value) {
        if (null != converter) {
            return converter.convert(fieldName,value.toString());
        } else {
            return value.toString();
        }
    }


}
